<?php

namespace App\Http\Controllers\App\V1;

use App\Exceptions\BasicException;
use App\Response\Response;
use App\Http\Controllers\BaseController;
use App\Services\Tools\CurlService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class ProtectController extends BaseController
{

    public function rewardInfo()
    {
        return Response::sendData([
            "status" => app("repo_protect")->getReceiveStatus(UID),
            "degree_desc" => app("repo_protect")->formatReceiveDesc(UID),
            "limit" => app("repo_protect")->getRewardLimit(),
            "degree" => app("repo_protect")->getActiveProtectorNumber(UID),
            "reward_list" => app("repo_protect")->getActiveRewardList(),
        ]);
    }

    public function groupMemberList(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }
        $userId = $request->input("user_id");
        $userId = empty($userId) ? UID : $userId;
        $userId = getRealUserId($userId);
        $result = app("repo_protect")->getGroupActiveList([
            "user_id" => $userId,
            "perPage" => $request->input("perPage"),
            "page" => $request->input("page"),
        ]);

        return Response::sendList($result,[], [
            "total_number" => app("repo_protect")->getAllProtectorNumber($userId),
            "active_total_number" => app("repo_protect")->getActiveProtectorNumber($userId),
        ]);
    }

    public function userProtectData(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'user_id' => 'Required|numeric|exists:mysql.ln_user,user_id',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $userId = $request->input("user_id");
        $groupUserList = app("repo_protect")->getGroupActiveList([
            "user_id" => $userId,
        ]);

        $expireTime = app("repo_protect")->getUserProtectExpireTime(UID, $userId);
        $expireStr = empty($expireTime) ? "" : date("Y年m月d日H:i:s后失效", $expireTime);

        $userInfo = app("repo_user")->getUserInfo($userId);

        return Response::sendData([
            "user_list" => array_slice($groupUserList->items(), 0, 3),
            "reward_list" => app("repo_protect")->getAddProtectRewardList(),
            "cur_user_data" => [
                "nickname" => $userInfo['nickname'] ?? "",
                "head_picture" => $userInfo['head_picture'] ?? ''
            ],
            "status" => app("repo_protect")->getProtectStatus(UID, $userId),
            "total_number" => app("repo_protect")->getAllProtectorNumber($userId),
            "protect_card_balance" => app("repo_protect")->getUserProtectCardNumber(UID),
            "expire_time" => $expireStr,
        ]);
    }

    public function useProtectCard(Request $request)
    {
        $userId = $request->input("user_id");
        if(empty($userId) || intval($userId) != $userId){
            throw new BasicException(10001, '参数错误');
        }
        $toUserInfo = app('model_user')->where(['user_id'=>$userId])->get(['user_id']);
        if(empty($toUserInfo)){
            throw new BasicException(10001, '用户不存在');
        }
        app("repo_protect")->useProtectCard(UID, $userId);

        return Response::sendData(true);
    }

    public function receiveProtectReward(Request $request)
    {
        app("repo_protect")->receiveReward(UID);

        return Response::sendData(true);
    }

}
